package com.longteng.java.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.longteng.java.web.dataproducer.DataInfo;
import com.longteng.java.web.dataproducer.TestDataGenerator;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by tianqing.wang
 * DATE : 2018/3/25
 * TIME : 上午12:46
 * PROJECT : web-lesson
 * PACKAGE : com.longteng.java.web
 *
 * @author <a href="mailto:celeskyking@163.com">tianqing.wang</a>
 */
@RestController
@RequestMapping("/test")
public class TestController {


    @Resource
    //@Autowired
    private RandomHelper randomHelper;

    @Resource
    private TestDataGenerator testDataGenerator;


    @RequestMapping(value = "/random",method = RequestMethod.GET)
    public String testIoc(){
        return String.valueOf(randomHelper.getRandom(1000,9999));
    }


    @GetMapping(value = "/testdata.json",produces = {"application/json"})
    public String testData(){
        return JSON.toJSONString(testDataGenerator.generate(10));
    }

    @GetMapping(value = "/data", produces={"application/json;charset=utf-8"})
    public List<DataInfo> testData(@RequestParam(value = "size",defaultValue = "10")Integer size){
        return testDataGenerator.generate(size);
    }



    @GetMapping("/test/demo.json")
    public String testParams(@RequestParam("name") String name,
                             @RequestParam("age") String age,
                             @RequestParam(value = "something",required = false) String something){
        System.out.println("name:"+name+",age:"+age+",something:"+something);
        return "hello, world";
    }


    @GetMapping(value = "/demo.json",produces = {"text/plain"})
    public String testWebParams( HttpServletRequest request, HttpServletResponse httpServletResponse){
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String something = request.getParameter("something");
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(age)||StringUtils.isEmpty(something)){
            httpServletResponse.setStatus(400);
            return "";
        }
        System.out.println("name:"+name+",age:"+age+",something:"+something);
        return "hello,world";
    }


    @PostMapping(value = "/body.json",produces = {"application/json;charset=utf-8"})
    public User testBody(@RequestBody User user){
        System.out.println(user.toString());
        user.setName("MR "+user.getName());
        return user;
    }


    @PostMapping(value = "/{name}/info.json",produces = {"application/json;charset=utf-8"})
    public User testPath (@PathVariable("name")String name){
        User user = new User();
        user.setName(name);
        user.setAge("30");
        user.setId("1");
        return user;
    }





    public static class User {

        @JSONField(name = "name")
        private String name;

        private String age;

        private String id;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return new ToStringBuilder(this)
                    .append("name", name).append("\n")
                    .append("age", age).append("\n")
                    .append("id", id).append("\n")
                    .toString();
        }
    }

}
